<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;

/**
 * bank_sign_log action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class BankSignLogAction extends CommonAction {

    protected $serviceBean = 'user.bankSignLog.service';
    protected $actionTitle = '银行签约日志';

    //数据列表页面
    public function index( HttpRequest $request ) {

        $mobile = $request->getParameter('mobile', 'trim');
        $signType = $request->getParameter('sign_type', 'trim');
        $starttime = $request->getParameter('starttime', 'trim');
        $endtime = $request->getParameter('endtime', 'trim');

        $conditions = array();
        if ( $mobile ) $conditions['mobile'] = "%{$mobile}%";
        if ( $signType ) $conditions['sign_type'] = $signType;
        if ( $starttime ) $conditions['addtime']['>'] = $starttime.' 00:00:00';
        if ( $endtime ) $conditions['addtime']['<'] = $endtime.' 24:00:00';

        $this->setConditions($conditions);
        $this->assign('tableHeader', array('会员号','签约类型','开户名称','支付通道', '银行卡号','附属帐号','开户银行','开户支行','签约时间'));
        if ( $request->getParameter('__export') == 1 ) {
            $this->export($request); //导出数据

        } else {
            $this->setOrder(array('addtime' => -1));
            parent::index($request);
            $this->setView('log/banksign_log');
            $this->assign('breadcrumb', array('日志管理', '签约日志'));
        }
    }

    //导出数据
    protected function export(HttpRequest $request) {
        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $service = Beans::get($this->getServiceBean());
            $items = $service->where($this->getConditions())->limit(1, $this->getPagesize())->sort(array('addtime' => -1))->find();
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["mobile"];
                $tmp[] = $v['sign_type'] == 1 ? '签约' : '解约';
                $tmp[] = $v["acc_name"];
                $tmp[] = $v["card_no"];
                $tmp[] = $v["virtual_account"];
                $tmp[] = $v["bank_name"];
                $tmp[] = $v["sign_bank_branch"];
                $tmp[] = $v["addtime"];
                $excel[] = $tmp;
            }
            $header = $this->getTemplateVar('tableHeader');
            phpexcel("签约日志",$header,$excel);
            exit();
        }
    }
}
